<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*------------------------------
  Programmer: Nguyen Dang Khoa
------------------------------*/

class Cpanel extends Controller {

	function Cpanel()
	{
		parent::Controller();
	}
	
	function index()
	{
		$data['icon']="";
		$data['title']="";
		$data['content']=$this->load->view('main','',true);
		$this->load->view('template',$data);
	}
	function changepassword()
	{
		$data['icon']="icon-48-job-changepass.png";
		$data['title']=$this->lang->line('changepass');

		$data['content']=$this->load->view('changepassword','',true);
		$this->load->view('template',$data);
	}
// =============== GLOBAL CONFIG ==============================	
	function globalconfig()
	{
		$data['icon']="icon-48-config.png";
		$data['title']=$this->lang->line('m_globalconfig');

		$data['content']=$this->load->view('globalconfig','',true);
		$this->load->view('template',$data);
	}
	function update_config()
	{
		$this->load->helper('file');

		$address=str_replace("'","&acute;",$_POST['menu_frontpage']);
		$address=str_replace('"',"&quot;",$address);
		
		$data='<?php $menu_frontpage="'.$address.'";';
		$result=write_file('./parameter.php', $data,'w+');
		
		if ($result)
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/globalconfig/");
	}
// =============== NEWSLETTER ==============================	
	function newsletter()
	{
		$data['icon']="icon-48-newsletter.png";
		$data['title']="Newsletter";
		$_newsletter= array();
		$this->load->library('pagination');
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/newsletter/';
		$config['total_rows'] =$this->general_model->get_newsletter('totalrow','','');
		$config['per_page'] =20;
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);
		$_newsletter=array();
		$result_newsletter=$this->general_model->get_newsletter('',$config['per_page'],$this->uri->segment(4));
		if($result_newsletter->num_rows()>0)
		{
			$_newsletter['newsletter']=$result_newsletter->result_array();
		}
		//var_dump($result_newsletter);
		$data['content']=$this->load->view('newletter',$_newsletter,true);
		$this->load->view('template',$data);
	}
	
	function delete_newsletter($id)
	{
		
		if ($this->general_model->delete_newsletter($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/newsletter/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/newsletter/");
		}
	}

	function sendmail_newsletter()
	{
		
		if ($this->general_model->sendmail_newsletter($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/newsletter/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/newsletter/");
		}
	}
// =================== FAQ MANAGER ============	

	function faq()
	{
		$data['icon']="icon-48-faq.png";
		$data['title']="FAQ";
		$result_faq_manager=$this->general_model->get_faq_manager_all(2);
		$faq_manager=array();
		if($result_faq_manager->num_rows()>0)
		{
			$faq_manager=$result_faq_manager->result_array();

		}
		$_faqdata['faq_manager']=$faq_manager;
		$_faqdata['is_company']='';
		$_faqdata['is_jobseeker']='';
		$data['content']=$this->load->view('faq_manager',$_faqdata,true);
		$this->load->view('template',$data);
	}

	function delete_faq($id)
	{
		if ($this->general_model->delete_faq($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/faq/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/faq/");
		}
	}
// ======== REVIEWS MANAGER =================

	function reviews_manager()
	{
		$data['icon']="icon-48-article.png";
		$data['title']="REVIEWS";
		$result_faq_manager=$this->general_model->get_faq_manager_all(1);
		$faq_manager=array();
		if($result_faq_manager->num_rows()>0)
		{
			$faq_manager=$result_faq_manager->result_array();

		}
		$_faqdata['faq_manager']=$faq_manager;
		$data['content']=$this->load->view('reviews_manager',$_faqdata,true);
		$this->load->view('template',$data);
	}

	function delete_reviews($id)
	{
		if ($this->general_model->delete_faq($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/reviews_manager/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/reviews_manager/");
		}
	}
//==================== ARTICLE======================	
	function vin_manager()
	{
		$data['icon']="icon-48-article.png";
		$data['title']="Vin Manager";
		
		$_newsletter= array();
		$this->load->library('pagination');
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/vin_manager/';
		$config['total_rows'] =$this->general_model->get_totalrow_article(2);
		$config['per_page'] =10;
		//echo $config['per_page'];
		$config['uri_segment'] = 4; 
		//echo $config['total_rows'];
		$this->pagination->initialize($config);
		
		$result_newsletter=$this->general_model->get_all_article(2,$config['per_page'],$this->uri->segment(4));
		if($result_newsletter->num_rows()>0)
		{
			$_newsletter['newsletter']=$result_newsletter->result_array();
		}
		
		$data['content']=$this->load->view('vin_manager',$_newsletter,true);
		$this->load->view('template',$data);

	}
	
	function vin_manipulate()
	{
		if ($_POST['option']=='add_article_item') // addnew
		{
			$this->add_vin($_POST);
		}
		else // update
		{
			$this->update_vin($_POST);			
		}
	}
	function add_vin($arr)
	{
		
		if ($this->general_model->add_article(2,$arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/vin_manager/");
	}
	function update_vin($arr)
	{
		
		if ($this->general_model->update_article($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	   		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/vin_manager/");
	}
	function delete_vin($id)
	{
		if ($this->general_model->delete_search_job($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/vin_manager/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/vin_manager/");
		}
	}
	function search_job_manager()
	{
		$data['icon']="icon-48-article.png";
		$data['title']="Search Job Manager";
		
		$_newsletter= array();
		$this->load->library('pagination');
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/search_job_manager/';
		$config['total_rows'] =$this->general_model->get_totalrow_article(1);
		$config['per_page'] =10;
		//echo $config['per_page'];
		$config['uri_segment'] = 4; 
		//echo $config['total_rows'];
		$this->pagination->initialize($config);
		
		$result_newsletter=$this->general_model->get_all_article(1,$config['per_page'],$this->uri->segment(4));
		if($result_newsletter->num_rows()>0)
		{
			$_newsletter['newsletter']=$result_newsletter->result_array();
		}
		
		$data['content']=$this->load->view('searchjob_manager',$_newsletter,true);
		$this->load->view('template',$data);

	}
	function delete_search_job($id)
	{
		if ($this->general_model->delete_search_job($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/search_job_manager/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/search_job_manager/");
		}
	}
	function get_article_detail($id)
	{
		$result_article_detail=$this->general_model->get_article_detail($id);
		if($result_article_detail->num_rows()>0)
		{
			
			echo json_encode($result_article_detail->result_array());
		}
	}
	function article_manipulate()
	{
		if ($_POST['option']=='add_article_item') // addnew
		{
			$this->add_article($_POST);
		}
		else // update
		{
			$this->update_article($_POST);			
		}
	}
	function add_article($arr)
	{
		
		if ($this->general_model->add_article(1,$arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/search_job_manager/");
	}
	function update_article($arr)
	{
		
		if ($this->general_model->update_article($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	   		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/search_job_manager/");
	}
	
//  ============ HISTORY ==================

	function history()
	{
		$data['icon']="icon-48-article.png";
		$data['title']="HISTORY";
		$result_faq_manager=$this->general_model->get_information(1);
		$faq_manager=array();
		if($result_faq_manager->num_rows()>0)
		{
			$faq_manager=$result_faq_manager->result_array();

		}
		$_faqdata['information_manager']=$faq_manager;
		$data['content']=$this->load->view('information_manager',$_faqdata,true);
		$this->load->view('template',$data);
	}	
	function update_history()
	{
		if ($this->general_model->update_history($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	   		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/history/");
	}
	
// =================== GALLERY - SLIDESHOW =====================

	function slideshow()
	{
		$data['icon']="icon-48-media.png";
		$data['title']='Slideshow';
		
		$slidehow=array();
		$result_slidehow=$this->general_model->get_slideshow();
		if($result_slidehow->num_rows()>0)
		{
			$slidehow=$result_slidehow->result_array();
		}
		$_menudata['slideshow']=$slidehow;
		$data['content']=$this->load->view('slideshow',$_menudata,true);
		$this->load->view('template',$data);
	}
	function add_slideshow()
	{
		if ($this->general_model->add_slideshow($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/slideshow/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/slideshow/");
		}
	}
	function delete_slideshow($id)
	{
		if ($this->general_model->delete_slideshow($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/slideshow/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/slideshow/");
		}

	}	
	
	function gallery()
	{
		$data['icon']="icon-48-media.png";
		$data['title']="Gallery";
		
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/gallery/';
		$config['total_rows'] =$this->general_model->get_totalrow_gallery();
		$config['per_page'] = '8';
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);
		
		$gallery=array();
		$result_gallery=$this->general_model->get_gallery($config['per_page'],$this->uri->segment(4));
		if($result_gallery->num_rows()>0)
		{
			$gallery=$result_gallery->result_array();
		}
		$_menudata['gallery']=$gallery;
		$_menudata['all_link']=$this->pagination->create_links();
		$data['content']=$this->load->view('gallery',$_menudata,true);
		$this->load->view('template',$data);
	}
	
	function add_gallery()
	{
		if ($this->general_model->add_gallery($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/gallery/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/gallery/");
		}
	}
	function delete_gallery($id)
	{
		if ($this->general_model->delete_gallery($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/gallery/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/gallery/");
		}

	}	
//=============  MENU MANAGER ====================
	function menu_manager()
	{
		$data['icon']="icon-48-article.png";
		$data['title']="Menu Manager";
		
		$_newsletter= array();
		$this->load->library('pagination');
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/menu_manager/';
		$config['total_rows'] =$this->general_model->get_totalrow_article(3);
		$config['per_page'] =10;
		//echo $config['per_page'];
		$config['uri_segment'] = 4; 
		//echo $config['total_rows'];
		$this->pagination->initialize($config);
		
		$result_newsletter=$this->general_model->get_all_article(3,$config['per_page'],$this->uri->segment(4));
		if($result_newsletter->num_rows()>0)
		{
			$_newsletter['newsletter']=$result_newsletter->result_array();
		}
		
		$data['content']=$this->load->view('menu_manager',$_newsletter,true);
		$this->load->view('template',$data);

	}
	
	function menu_manipulate()
	{
		if ($_POST['option']=='add_article_item') // addnew
		{
			$this->add_menu($_POST);
		}
		else // update
		{
			$this->update_menu($_POST);			
		}
	}
	function add_menu($arr)
	{
		
		if ($this->general_model->add_article(3,$arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/menu_manager/");
	}
	function update_menu($arr)
	{
		
		if ($this->general_model->update_article($arr))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	   		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/menu_manager/");
	}
	function delete_menu($id)
	{
		if ($this->general_model->delete_search_job($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		redirect("/adminpage/cpanel/menu_manager/");
	}
/*	function menu_manager($id='')
	{
		$data['icon']="icon-48-menumgr.png";
		
			$data['title']="Menu Manager";
			$job_category_manager=array();
			if ($id!='')
			{
				$result_job_category_manager=$this->general_model->get_menu_manager($id); // MENU ITEM in CATEGORY
			}
			else
			{
				$result_job_category_manager=$this->general_model->get_menu_manager('');// CATEGORY
			}
			if($result_job_category_manager->num_rows()>0)
			{
				
				$job_category_manager=$result_job_category_manager->result_array();
			}
			$_job_categorydata['menu_manager']=$job_category_manager;
			$data['content']=$this->load->view('menu_manager',$_job_categorydata,true);
				
		$this->load->view('template',$data);
	}	
	function addnew_menu()
	{
		if ($this->general_model->addnew_menu($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect($_POST['myurl']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect($_POST['myurl']);
		}
	}

	function delete_menu($id,$category_id='')
	{
		if ($this->general_model->delete_menu($id,$category_id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
		}
		if ($category_id >0)
		{
			redirect("/adminpage/cpanel/menu_manager/".$category_id);
		}
		else
		{
			redirect("/adminpage/cpanel/menu_manager/");
		}

	}	
	

	function show_menu_detail($id,$category='')
	{
		$data['is_category']='';
		if ($category!='')
		{
			$result_edit_menu=$this->general_model->get_edit_menu_detail($id,$category); // MENU
			$data['is_category']=0;
		}
		else
		{
			$result_edit_menu=$this->general_model->get_edit_menu_detail($id); // CATEGORY
			$data['is_category']=1;
		}
		$data['edit_menu']=array();
		
		if($result_edit_menu->num_rows()>0)
		{
			$data['edit_menu']=$result_edit_menu->result_array();
		}
		//var_dump($result_edit_menu);
		$this->load->view('popup_edit_menu',$data);
	}
	function update_menu()
	{
		if ($this->general_model->update_menu($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/menu_manager/".$_POST['is_category']);
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/menu_manager/".$_POST['is_category']);
		}
	}
*/


// ==========================RESERVATION=========================	
	
	function reservations_manager()
	{
		$data['icon']="icon-48-cv-manager.png";
		$data['title']="Reserve manager";
		if (isset($_POST['search_option']))
		{
			$this->session->set_userdata('search_option_cv',$_POST['search_option']);
		}
		if (isset($_POST['keyword']))
		{
			$this->session->set_userdata('keyword_cv',$_POST['keyword']);
		}

		$this->load->library('pagination');
		$config['base_url'] = base_url().index_page().'/adminpage/cpanel/reservations_manager/';
		$config['total_rows'] =$this->general_model->get_reservations('totalrow',$this->session->userdata('search_option_cv'),$this->session->userdata('keyword_cv'),'','');
		$config['per_page'] = 6;
		$config['uri_segment'] = 4; 
		//echo $config['total_rows'];
		$this->pagination->initialize($config);
		//$segment=$this->uri->segment(4, 0);		// asign= 0 if NOT found
		$result_reservations=$this->general_model->get_reservations('',$this->session->userdata('search_option_cv'),$this->session->userdata('keyword_cv'),$config['per_page'],$this->uri->segment(4));
		
		$reservations=array(); //  incase NO RECORD found
		
		if($result_reservations->num_rows()>0)
		{
			$reservations=$result_reservations->result_array();
		}
		$_data['reservations']=$result_reservations->result_array();
		$data['content']=$this->load->view('reservation_manager',$_data,true);
		//var_dump ($_videodata);
		$this->load->view('template',$data);
	}
	function delete_reserve($id)
	{
		if ($this->general_model->delete_reserve($id))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/reservations_manager/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/reservations_manager/");
		}

	
	}
	function show_status_reserve($id,$status)
	{
		$data['id']=$id;
		$data['status']=$status;
		$this->load->view('popup_edit_reserve',$data);
	}
	function update_status_reserve()
	{
		if ($this->general_model->update_status_reserve($_POST))
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_success').'</div>');	    	
			redirect("adminpage/cpanel/reservations_manager/");
		}
		else
		{
			$this->session->set_flashdata('message', '<div id="flashmessage">'.$this->lang->line('action_error').'</div>');
			redirect("/adminpage/cpanel/reservations_manager/");
		}

	}
}
